//持仓持续刷新
var datapx=0;
setInterval(function(){
	for(var i=0;i<datapx;i++){
		var times=parseInt($('#profile').find('.panel').eq(i).find('.publictime').html()),
			now=parseFloat($('#price').html()),
			buy=parseFloat($('#profile').find('.panel').eq(i).find('.publicbuy').html()),
			direction=$('#profile').find('.panel').eq(i).find('.publicdirection').html(),
			lang=parseInt($('#profile').find('.panel').eq(i).find('.progress-bar').attr('role-lang'));
		times--;
		if(times<0){
			datapx-=1;
			$('#profile').find('.panel').eq(i).remove();
		}else{
			$('#profile').find('.panel').eq(i).find('.publictime').html(times);
			$('#profile').find('.panel').eq(i).find('.publicpoint').html($('#price').html());
			//$('#profile').find('.panel').eq(i).find('.publicpoint').html(now);
			$('#profile').find('.panel').eq(i).find('.progress-bar').css('width',((lang-times)/lang*100)+'%')
			if(direction=='买涨'){
				if(now>buy){
					$('#profile').find('.panel').eq(i).find('.publicresult').html('盈').addClass('publicred').removeClass('publicgreen').removeClass('publicyellow')
				}else if(now==buy){
					$('#profile').find('.panel').eq(i).find('.publicresult').html('平').addClass('publicyellow').removeClass('publicred').removeClass('publicgreen')
				}else{
					$('#profile').find('.panel').eq(i).find('.publicresult').html('亏').addClass('publicgreen').removeClass('publicred').removeClass('publicyellow')
				}
			}else{
				if(now>buy){
					$('#profile').find('.panel').eq(i).find('.publicresult').html('亏').addClass('publicgreen').removeClass('publicred').removeClass('publicyellow')
				}else if(now==buy){
					$('#profile').find('.panel').eq(i).find('.publicresult').html('平').addClass('publicyellow').removeClass('publicred').removeClass('publicgreen')
				}else{
					$('#profile').find('.panel').eq(i).find('.publicresult').html('盈').addClass('publicred').removeClass('publicgreen').removeClass('publicyellow')
				}
			};
		}
//		console.log(now+'/'+buy);
//		console.log(direction);
	}
},1000);
//切换持仓和结算ajax加载刷新
$('#myTab').find('li').each(function(){
	$(this).click(function(){
		var rr=$(this).index();
		if(rr==1){
			succs();
			$('.business-fixed').hide()
		}else if(rr==2){
			messages(1);
			pageindex=1;
			$('.business-fixed').hide()
		}else if(rr==0){
			$('.business-fixed').show()
		}
	})
})
//持仓AJAX
function succs(){
	$.ajax({
		type:"post",
		data:{
			'_token':token,
			},
		dataType:'json',
		url:positionurl,
		//async:true,
		success:function(data){
//			console.log(data)
			datapx=data.length;
			profileadd(data);
		},
		error:function(e){
			console.log(e);
		}
	})
}
//持仓ajax
function profileadd(data){
	$('#profile').html('');
	var ho='';
	$.map(data, function(val,i) {
		var direction=val['direction']==1 ? "买涨" : "买跌" ;
		if(val['direction']==1){
			ho+="<div class='panel panel-default'>"+
	  	   "<div class='panel-heading'><span class='publicred publicdirection'>"+direction+"</span>:"+val['option_name']+"<span class='right'>"+val['created_at']+"</span></div>"+
	  	   "<div class='panel-body'>"+
	       "<div class='clearfix option'><span class='left'>金额:<b>"+val['amount']+"</b>元</span><span class='right'>执行价<b class='publicbuy'>"+val['buy_point']+"</b></span></div>"+
	       "<div class='clearfix option'><span class='left'>收益:<b>"+val['rate']+"%</b></span><span class='right'>当前价<b class='publicpoint'>"+val['close_point']+"</b></span></div>"+
	       "<div class='clearfix option'><span class='left'>剩余:<b class='publictime'>"+val['countdown']+"</b></span><span class='right'>预测结果:<b class='publicresult'>盈</b></span></div>"+
	       	"<div class='progress'>"+
			"<div class='progress-bar progress-bar-striped active' role-lang="+val['times']+" role='progressbar' aria-valuenow='1' aria-valuemin='0' aria-valuemax='100' style='width: 1%'>"+
			"<span class='sr-only'>45% Complete</span>"+
			"</div>"+
			"</div>"+
	       "</div>"+
	       "</div>";
		}else{
			ho+="<div class='panel panel-default'>"+
	  	   "<div class='panel-heading'><span class='publicgreen publicdirection'>"+direction+"</span>:"+val['option_name']+"<span class='right'>"+val['created_at']+"</span></div>"+
	  	   "<div class='panel-body'>"+
	       "<div class='clearfix option'><span class='left'>金额:<b>"+val['amount']+"</b>元</span><span class='right'>执行价<b class='publicbuy'>"+val['buy_point']+"</b></span></div>"+
	       "<div class='clearfix option'><span class='left'>收益:<b>"+val['rate']+"%</b></span><span class='right'>当前价<b class='publicpoint'>"+val['close_point']+"</b></span></div>"+
	       "<div class='clearfix option'><span class='left'>剩余:<b class='publictime'>"+val['countdown']+"</b></span><span class='right'>预测结果:<b class='publicresult'>盈</b></span></div>"+
	       	"<div class='progress'>"+
			"<div class='progress-bar progress-bar-striped active' role-lang="+val['times']+" role='progressbar' aria-valuenow='1' aria-valuemin='0' aria-valuemax='100' style='width: 1%'>"+
			"<span class='sr-only'>45% Complete</span>"+
			"</div>"+
			"</div>"+
	       "</div>"+
	       "</div>";
		}
	});
	$('#profile').append(ho);
}
//结算ajax
var pageindex=1;
function messages(indexpage){
	$.ajax({
		type:"post",
		data:{
			'_token':token,page:indexpage
			},
		dataType:'json',
		url:settlementurl,
		//async:true,
		success:function(data){
//			console.log(data)
			messagesadd(data['orders']['data']);
			$('.noe-ws').find('span').find('i').html(data['user']['balance'])
		},
		error:function(e){
			console.log(e);
		}
	});//发送
}
function messagesadd(data){
	if(pageindex==1){
		$('#messages').find('div').eq(0).find('div').remove();
//		console.log('s')
	};
	var ho='';
	$.map(data, function(val,i) {
		var direction=val['direction']==1 ? "买涨" : "买跌" ;
		if(val['direction']==1){
			ho+="<div class='panel panel-default'>"+
	  		"<div class='panel-heading'><span class='publicred publicdirection'>"+direction+"</span><span class='right chaoguo'>"+val['orderno']+"</span></div>"+
	  		"<div class='panel-body'>"+
	     	"<div class='clearfix option'><span class='left'>金额:<b>"+val['amount']+"</b></span><span class='right'>执行价:<b class='publicbuy'>"+val['buy_point']+"</b></span></div>"+
	     	"<div class='clearfix option'><span class='left'>收益:<b>"+val['rate']*100+"%</b></span><span class='right'>结算价:<b class='publicpoint'>"+val['close_point']+"</b></span></div>"+
	     	"<div class='clearfix option'><span class='left'>时长:<b class='publictime'>"+val['times']+"</b></span><span class='right'>盈亏:<b class='publicresult'>"+val['profit']+"</b></span></div>"+
	     	"<div class='clearfix option'><span>开始时间："+val['created_at']+"</span></div>"+
	     	"<div class='clearfix option'><span>结束时间："+val['close_time']+"</span></div>"+
	 		"</div>"+
			"</div>";
		}else{
			ho+="<div class='panel panel-default'>"+
	  		"<div class='panel-heading'><span class='publicgreen publicdirection'>"+direction+"</span><span class='right chaoguo'>"+val['orderno']+"</span></div>"+
	  		"<div class='panel-body'>"+
	     	"<div class='clearfix option'><span class='left'>金额:<b>"+val['amount']+"</b></span><span class='right'>执行价:<b class='publicbuy'>"+val['buy_point']+"</b></span></div>"+
	     	"<div class='clearfix option'><span class='left'>收益:<b>"+val['rate']*100+"%</b></span><span class='right'>结算价:<b class='publicpoint'>"+val['close_point']+"</b></span></div>"+
	     	"<div class='clearfix option'><span class='left'>时长:<b class='publictime'>"+val['times']+"</b></span><span class='right'>盈亏:<b class='publicresult'>"+val['profit']+"</b></span></div>"+
	     	"<div class='clearfix option'><span>开始时间："+val['created_at']+"</span></div>"+
	     	"<div class='clearfix option'><span>结束时间："+val['close_time']+"</span></div>"+
	 		"</div>"+
			"</div>";
		}
	});
	$('#messages').find('div').eq(0).append(ho);
}
//结算加载更多按钮
$('#addmore').click(function(){
	pageindex=pageindex+1
	messages(pageindex)
})
//页面点数持续刷新
function ptinit(data,i){
	var upmm=(parseFloat(data['last_price'])-parseFloat(data['open_price'])).toFixed(i);
	//var lastprice = parseFloat(data['last_price']);
	if(parseFloat(data['last_price'])){
		upsmm=((parseFloat(data['last_price'])-parseFloat(data['open_price']))*100/parseFloat(data['open_price'])).toFixed(i);
	}else{
		upsmm=parseFloat(0).toFixed(i);
	}
        
    price.html(data['last_price']);
    $('.timeshow').find('.m3').find('span').html(data['last_price']);
    if(upmm>0){
   		up.html(upmm).addClass('publicred').removeClass('publicgreen');
    }else{
   		up.html(upmm).addClass('publicgreen').removeClass('publicred');
    }
    if(upsmm>0){
   		ups.html(upsmm).addClass('publicred').removeClass('publicgreen');
    }else{
   		ups.html(upsmm).addClass('publicgreen').removeClass('publicred');
    }
    if(data['sell_price']>data['open_price']){
   		BP.html(data['sell_price']).addClass('publicred').removeClass('publicgreen');
    }else{
   		BP.html(data['sell_price']).addClass('publicgreen').removeClass('publicred');
    }
    if(data['buy_price']>data['open_price']){
   		SP.html(data['buy_price']).addClass('publicred').removeClass('publicgreen');
    }else{
   		SP.html(data['buy_price']).addClass('publicgreen').removeClass('publicred');
    }
    if(zhangdie==0){
    	$('#buynowshow').html(data['buy_price'])
    }else{
    	$('#buynowshow').html(data['sell_price'])
    }
}
//下单ajax
function bayajax(){
	var buy_point = $('#buynowshow').html();
	var duration = parseInt($('#dLabelx').find('.itemx').html().replace(/[^0-9]/ig,""));
	var amount = parseInt($('#dLabels').find('.itemx').html().replace(/[^0-9]/ig,""));
	var yield_rate = temp[parseInt($('#dLabelx').find('.itemx').html().replace(/[^0-9]/ig,""))];
	$('#risesend').attr('disabled','disabled');
	$('#fallsend').attr('disabled','disabled');
	$.ajax({
		type:"post",
		data:{
			'_token':token,
			option_id:heuyueid,
			direction:zhangdie,
			buy_point:buy_point,
			times:duration,
			amount:amount,
			yield_rate:yield_rate
		},
		url:urlgt,
		//async:true,
		success:function(data){
//			console.log(data)
			if(data.status==1){
				$('.err1').html('订单状态');
				$('.err2').html('下单成功');
				$('.timehide').hide();
				$('.timeshow').hide();
				$('.success-go').show();
				$('.error').show();
				$('#risesend').removeAttr('disabled');
				$('#fallsend').removeAttr('disabled');
				$('.noe-ws').find('span').find('i').html(data['user']['balance']);
			}else{
				$('.err1').html('下单失败');
				$('.err2').html(data['msg'][0]);
				$('.timehide').hide();
				$('.timeshow').hide();
				$('.error').show();
			}
			
		},
		error:function(e){
			console.log(e);
		}
	});//发送
}
//错误面板按钮
$('.err3').click(function(){
	$('.error').hide();
	$('.timeshow').hide();
	$('#risesend').removeAttr('disabled');
	$('#fallsend').removeAttr('disabled');
	$('#myModal').modal('hide');
	$('.timehide').show();
})

$('.success-click').click(function(){
	$('.error').hide();
	$('.timeshow').hide();
	$('#risesend').removeAttr('disabled');
	$('#fallsend').removeAttr('disabled');
	$('#myModal').modal('hide');
	$('.timehide').show();
	$('#myTab li:eq(1) a').tab('show');
	succs();
	$('.business-fixed').hide()
})
//下单成功面板再次下单按钮
$('.m6').click(function(){
	$('.timeshow').hide();
	$('.timehide').show();
	$('#risesend').removeAttr('disabled');
	$('#fallsend').removeAttr('disabled');
	clearTimeout(setout);
})
//弹窗关闭按钮
$('#myModal').on('hide.bs.modal', function (e) {
	$('.timeshow').hide();
	$('.timehide').show();
})
//发送按钮
var zhangdie;//涨跌标记
$('#rise').click(function(){
	zhangdie=1;
	$('.fallsend').hide();
	$('.timeshow').hide();
	$('.error').hide();
	
	$('#risesend').removeAttr('disabled');
	$('#fallsend').removeAttr('disabled');
	
	$('.timehide').show();
	$('.risesend').show();
	$('#myModal').modal('show');
})
$('#fall').click(function(){
	zhangdie=0;
	$('.risesend').hide();
	$('.timeshow').hide();
	$('.error').hide();
	
	$('#risesend').removeAttr('disabled');
	$('#fallsend').removeAttr('disabled');
	
	$('.timehide').show();
	$('.fallsend').show();
	$('#myModal').modal('show');
})
//下单的发送按钮
$('#risesend').click(function(){
	bayajax()
});
$('#fallsend').click(function(){
	bayajax()
});
//点数获取
function nowjson(i){
	
	var urltimeto =urltime +'?t='+ Math.random();
	$.getJSON(urltimeto, function(data){
//		console.log(data)
	   ptinit(data,i);
//	   console.log(data)
	});
};
//时间，金额选择变动
$('.business-hideli').each(function(){
	$(this).find('li').each(function(){
		$(this).click(function(){
			$(this).parent().parent().find('button').find('.itemx').html($(this).html());
			var second=parseInt($('#dLabelx').find('.itemx').html().replace(/[^0-9]/ig,""));
			var num   =parseInt($('#dLabels').find('.itemx').html().replace(/[^0-9]/ig,""));
//											var value = parseInt($('#dLabelx').find('.itemx').html().replace(/[^0-9]/ig,""))+parseInt($('#dLabels').find('.itemx').html().replace(/[^0-9]/ig,""));
			$('#rate').html(temp[second]*100);
			$('#ratex').html(num*temp[second]+num);
		})
	})
})